/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inovacao.dao;

import inovacao.entidades.Cliente;
import java.util.List;
import javax.management.Query;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Giovani
 */
public class ClienteDAO {

    public List<Cliente> findAll() {
     
        List<Cliente> clientes = null;
        //EntityManagerFactory emf = Persistence.createEntityManagerFactory("inovacaoPU");
        EntityManager em = new GenericDAO<Cliente>().getEm();
        em.getTransaction().begin();
        try {
             clientes = em.createNamedQuery("Cliente.findAll").getResultList();        
        } catch (Exception e) {
            em.getTransaction().rollback();
        } finally {            
            em.close();
            return clientes;
        }
    }
    

    public void persist(Cliente cliente) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("inovacaoPU");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(cliente);
            em.getTransaction().commit();
            em.createNativeQuery("Cliente.findAll");
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }
}
